from django.shortcuts import render
from application.models import bugStyle,bugs,projectInfo,caseDetail,caseInfo,userInfo,uploadImg
from xlwt import *
def addBugStylePage(request):
    bugStyles = bugStyle.objects.all();
    context = {}
    context['bugStyles'] = bugStyles
    return render(request, "admin/addBugTylePage.html", context);


def bugCheck(request):
    projectId = request.POST.get('projectId');
    stage = request.POST.get('stage');
    projects = projectInfo.objects.all().order_by("-id");
    context = {}
    bugList = []
    context['projects'] = projects;
    if projects==None or stage==None:
        return render(request, 'worker/bugCheckPage.html', context);
    if projects.count() > 0:
        project = projects[0];
        if projectId is None:
            projectId = project.id;
        context['projectId'] = int(projectId);
        bugz = bugs.objects.filter(projectId=projectId,stage=stage).order_by("caseId")
        for bug in bugz:
            bugDetail = {}
            caseTitleId = bug.caseId;
            caseDetailId = bug.caseDetailId;
            case = caseInfo.objects.get(id=caseTitleId);
            aCaseDetail = caseDetail.objects.get(id=caseDetailId);
            woker = userInfo.objects.get(id=bug.userId);
            imgId = bug.imgId;
            imgList = [];
            if imgId is not None:
                imgIds = str.split(imgId,',');
                imgList = uploadImg.objects.filter(id__in=imgIds);
            bugDetail['upFiles'] = imgList;
            bugDetail['bugInfo'] = bug;
            bugDetail['case'] = case;
            bugDetail['caseDetail'] = aCaseDetail;
            bugDetail['woker'] = woker;
            bugList.append(bugDetail);
    context['bugList'] = bugList;
    return render(request,'worker/bugCheckPage.html',context)

